//
//  NetworkingTools.m
//  Coding_My
//
//  Created by showXxie on 2017/8/21.
//  Copyright © 2017年 sooxie. All rights reserved.
//

#import "NetworkingTools.h"

@implementation NetworkingTools

+ (instancetype)share {
    static NetworkingTools *tools = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tools = [[NetworkingTools alloc] initWithBaseURL:[NSURL URLWithString:@"https://coding.net/"]];
        
        tools.responseSerializer = [AFHTTPResponseSerializer serializer];
        //self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json", @"text/html", nil];
        
        //        [self getNetWorkingStateWith:tools];
    });
    return tools;
}


- (void)requestMethod:(MethodType)method path:(NSString *)path withParams:(NSDictionary *)params andBlock:(void (^)(id, NSError *))block {
    
    if (path.length == 0) {
        return;
    }
   
    path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    
    switch (method) {
        case Get:{
            
            [self GET:path parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
        
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];

            DebugLog(@"\n===========response===========\n%@:\n%@", path, dic);
                
                block(dic, nil);

                
                
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
                block(nil,error);
                
            }];
            
            
            break;
        }
        case Post:{
            
            [self POST:path parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
           DebugLog(@"\n===========response===========\n%@:\n%@", path, dic);
                
                block(dic, nil);
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                block(nil,error);

            }];
            
            
            break;
        }
            
        default:
            break;
    }
    
    
}








@end
